package ams.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ams.dao.IBaseDao;
import ams.model.dto.ExecResult;
import ams.model.entity.Org;
import ams.util.SysConstans;

@Service("orgService")
public class OrgService implements IOrgService {

	
	@Autowired
	private IBaseDao<Org> orgDao;
	
	@Override
	public ExecResult add(Org org) {
		ExecResult execResult = new ExecResult();
		List<String> message = new ArrayList<>();
		
		if(org.getPid() == null){
			org.setPid(SysConstans.TREE_ROOT_ID);
		}
		
		Long sameCodecount = orgDao.count("select count(*) from Org o where o.code = ? and o.pid = ? ", new Object[]{org.getCode(),org.getPid()});
		Long sameNamecount = orgDao.count("select count(*) from Org o where o.name = ? and o.pid = ? ", new Object[]{org.getName(),org.getPid()});
		
		if(sameCodecount != 0){
			execResult.setSuccess(false);
			message.add("机构编码重复！");
			execResult.setMessage(message);
		}
		if(sameNamecount != 0){
			execResult.setSuccess(false);
			message.add("机构名称重复！");
			execResult.setMessage(message);
		}
		
		if(sameCodecount ==0 && sameNamecount==0){
			org.setState(SysConstans.TREEGRID_ISNOT_LEAF);
			orgDao.save(org);
			execResult.setSuccess(true);
		}
		
		return execResult;
	}

	@Override
	public ExecResult delete(Org org) {
		ExecResult execResult = new ExecResult();
		if(org.getId() != null){
			Long sonOrgCount = orgDao.count("select count(*) from Org o where o.pid = ?", new Object[]{org.getId()});
			if(sonOrgCount == 0){
				orgDao.executeHql("delete from Org o  where o.id = ?", new Object[]{org.getId()});
				execResult.setSuccess(true);
			}else{
				execResult.setSuccess(false);
				List<String> message = new ArrayList<>();
				message.add("删除的机构下存在子机构！");
				execResult.setMessage(message );
				
			}
		}
		return execResult;
	}

	@Override
	public ExecResult update(Org org) {
		ExecResult execResult = new ExecResult();
		List<String> message = new ArrayList<>();
		
		if(org.getPid() == null){
			org.setPid(SysConstans.TREE_ROOT_ID);
		}
		
		Long sameCodecount = orgDao.count("select count(*) from Org o where o.code = ? and o.id != ? ", new Object[]{org.getCode(),org.getId()});
		Long sameNamecount = orgDao.count("select count(*) from Org o where o.name = ? and o.id != ? ", new Object[]{org.getName(),org.getId()});
		
		if(sameCodecount != 0){
			execResult.setSuccess(false);
			message.add("机构编码重复！");
			execResult.setMessage(message);
		}
		if(sameNamecount != 0){
			execResult.setSuccess(false);
			message.add("机构名称重复！");
			execResult.setMessage(message);
		}
		
		if(sameCodecount ==0 && sameNamecount==0){
			org.setState(SysConstans.TREEGRID_ISNOT_LEAF);
			orgDao.update(org);
			execResult.setSuccess(true);
		}
		
		return execResult;
	}

	@Override
	public List<Org> getOrgByPid(Integer pid) {
		List<Org> orgList = new ArrayList<>();
		if(pid == null){
			orgList = orgDao.Find("from Org o where o.pid = 0");
		}else{
			orgList = orgDao.Find("from Org o where o.pid = ?" , new Object[]{pid});
		}
		return orgList;
	}

	@Override
	public List<Org> getOrgCatalog() {
		List<Org> orgList = new ArrayList<>();
		orgList = orgDao.Find("select new Org(id,pid,name,state) from Org ");
		return orgList;
	}
	
	

}
